package week7.jdbc;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;

public class HikariFactory implements ConnectionFactory {

    private static HikariDataSource ds;

    static {
        String configFile = Objects.requireNonNull(HikariFactory.class.getClassLoader().getResource("db.properties")).getPath();

        HikariConfig cfg = new HikariConfig(configFile);
        ds = new HikariDataSource(cfg);
    }

    @Override
    public Connection getConnection() {
        Connection conn = null;
        try {
            conn = ds.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conn;
    }
}
